package www.shw.com.rabbitmq;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import www.shw.com.bean.LvEx;
import www.shw.com.mapper.LvMapper;

//@Component
//@EnableScheduling
public class ConsumerTask {

	@Resource
	private RabbitTemplate rabbitTemplate;
	//定义json处理对象
	private ObjectMapper objectMapper = new ObjectMapper();
	//定义操作Mapper对象
	@Resource
	private  LvMapper lvMapper;
	
	@Scheduled(cron="*/10 * * * * ?")
	public void taskProcess() throws Exception {
		
		System.out.println("定时任务每隔 10s 执行一次@@@@@@@@@@"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())));
		
		//接收到消息
		Message message = rabbitTemplate.receive("myqueue");
		
		if(message!=null) {
			//读取json数据
			String json = new String(message.getBody());
			//转化成实体对象
			LvEx lv = objectMapper.readValue(json,LvEx.class);
			//执行保存操作
			
			lvMapper.insertMiddle(lv);
		}else {
			System.out.println("队列中无数据");
		}
	}
}
